/* 
 * function:管理后台中文js代码
 * date:2011-09-28
 * author: long@7road.com
 */

// 初始化日期时间
admin.ui.initDateTime = function() {
    function _getNum(num) {
        return num < 10 ? '0' + num : num;
    }
    function _getTime() {
        var date = new Date();
        var weeks = ['<span class="txt-orange">星期天</span>', '星期一', '星期二', '星期三', '星期四', '星期五', '<span class="txt-orange">星期六</span>'];
        $('#time').html(date.getFullYear() + '年' + _getNum(date.getMonth() + 1) + '月' + _getNum(date.getDate()) + '日 ' + weeks[date.getDay()] + ' ' + _getNum(date.getHours()) + ":" + _getNum(date.getMinutes()) + ":" + _getNum(date.getSeconds()));
    }
    if ($('#time').size() > 0) {
        _getTime();
        window.setInterval(_getTime, 1000);
    }
};

// 输出分页
admin.ui.pager = {};
admin.ui.outputPager = function(urlFormat, pageSize, pageIndex, pageCount, recordCount) {
    pageSize = parseInt(pageSize, 10);
    pageIndex = parseInt(pageIndex, 10);
    pageCount = parseInt(pageCount, 10);
    recordCount = parseInt(recordCount, 10);

    if (pageIndex < 1)
        pageIndex = 1;
    if (pageIndex > pageCount)
        pageIndex = pageCount;

    admin.ui.pager.urlFormat = urlFormat;
    admin.ui.pager.pageCount = pageCount;

    function _getLink(text, enabled, urlFormat, index) {
        if (enabled == false)
            return $.format(' <a class="button-white" style="filter:Alpha(Opacity=60);opacity:0.6;" href="javascript:void(0);"><span>{0}</span></a>', text);
        else
            return $.format(' <a class="button-white" href="' + urlFormat + '"><span>{1}</span></a>', index, text);
    }

    var html = [];
    html.push('<div class="pager-bar">');
    html.push($.format('<div class="msg">共{0}条记录，当前第{1}/{2}，每页{3}条记录</div>', recordCount, pageIndex, pageCount, pageSize));
    html.push(_getLink('首页', pageIndex > 1, urlFormat, 1));
    html.push(_getLink('上一页', pageIndex > 1, urlFormat, pageIndex - 1));
    html.push(_getLink('下一页', pageCount > 0 && pageIndex < pageCount, urlFormat, pageIndex + 1));
    html.push(_getLink('未页', pageCount > 0 && pageIndex < pageCount, urlFormat, pageCount));
    html.push('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
    html.push('第<input id="current-index" onkeydown="$.enterSubmit(event, admin.ui.pagerJump);" class="input-small" type="text" value="' + (pageIndex > 0 ? pageIndex : '') + '" />页');
    html.push('&nbsp;&nbsp;&nbsp;&nbsp;');
    html.push('<a class="button-white"' + (pageCount <= 1 ? ' style="filter:Alpha(Opacity=60);opacity:0.6;" href="javascript:void(0);"' : ' href="javascript:admin.ui.pagerJump();"') + '><span>跳转</span></a>');
    html.push('</div>');

    document.write(html.join(''));
};

admin.ui.doLogout = function() {
    if (typeof $.box != undefined) {
        $.box.confirm('确定要退出系统吗？', function(v, m, f) {
            location.href = admin.config.logoutUrl;
        });
    } else {
        if (confirm('确定要退出系统吗？')) {
            location.href = admin.config.logoutUrl;
        }
    }
};

admin.ui.doLogin = function() {
    var username = $('#username').trim();
    var password = $('#password').trim();

    if (username == '') {
        $('#login-tip').text('请输入用户！');
        $('#username').focus();
        return;
    }
    if (password == '') {
        $('#login-tip').text('请输入密码！');
        $('#password').focus();
        return;
    }
    var data = {
        username: username, 
        password: password
    };
    
    $('#login-tip').text('正在登录，请稍后...');
    $.doPost(admin.config.validloginUrl, data, function(result) {
        if (result) {
            if (result.success) {
                $('#login-tip').text('登录成功，请稍后...');
                location.href = admin.config.indexUrl;
            } else {
                if (result.data == 1) {
                    admin.ui.doRefreshCode();
                    $('#login-tip').text('验证码已失效，请重新输入！');
                    $('#verifycode').val('').focus();
                } else if (result.data == 2) {
                    admin.ui.doRefreshCode();
                    $('#login-tip').text('验证码错误，请重新输入！');
                    $('#verifycode').select();
                } else {
                    $('#login-tip').text('用户或密码错误，请重新输入！');
                    $('#username').select();
                }
            }
        }
    });
};

//用户列表
// 删除单个用户
function deleteUser(userName) {
    $.box.confirmx("删除用户", "确定要删除吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/user/deleteuser', {
            userName: userName
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        $("#tr_" + userName).hide().remove();
                    }
                });
            }
        });
    });
}
// 删除多个用户
function batchDeleteUser() {
    var userNames = [];

    $(".check-box").each(function() {
        var val = $(this).attr("rel");
        if (val == "admin")
            this.checked = false;
        if (this.checked)
            userNames.push(val);
    });

    if (userNames.length == 0) {
        $.box.alert("请选择至少一个用户！");
        return;
    }

    $.box.confirmx("删除用户", "确定要删除选中的用户吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/user/batchdeleteuser', {
            userNames: userNames.join(",")
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        $.each(userNames, function() {
                            $("#tr_" + this).hide().remove();
                        });
                    }
                });
            }
        });
    });
}



// 删除单个简报
function deleteBulletin(id) {
    $.box.confirmx("删除简报", "确定要删除吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/bulletin/delete', {
            id: id
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        $("#" + id).hide().remove();
                    }
                });
            }
        });
    });
}
// 删除多个用户
function batchDeleteBulletin() {
    var ids = [];

    $(".check-box").each(function() {
        var val = $(this).attr("rel");
        if (this.checked)
            ids.push(val);
    });

    if (ids.length == 0) {
        $.box.alert("请选择至少一个简报！");
        return;
    }

    $.box.confirmx("删除简报", "确定要删除选中的简报吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/bulletin/batchdelete', {
            ids: ids.join(",")
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        $.each(ids, function() {
                            $("#" + this).hide().remove();
                        });
                    }
                });
            }
        });
    });
}

// 删除单个游戏区
function deleteServer(id) {
    $.box.confirmx("删除游戏区", "确定要删除吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/server/delete', {
            id: id
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        $("#tr_" + id).hide().remove();
                    }
                });
            }
        });
    });
}
// 删除多个用户
function batchDeleteServer() {
    var ids = [];

    $(".check-box").each(function() {
        var val = $(this).attr("rel");
        if (this.checked)
            ids.push(val);
    });

    if (ids.length == 0) {
        $.box.alert("请选择至少一个游戏区！");
        return;
    }

    $.box.confirmx("删除游戏区", "确定要删除选中的游戏区吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/server/batchdelete', {
            ids: ids.join(",")
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        $.each(ids, function() {
                            $("#tr_" + this).hide().remove();
                        });
                    }
                });
            }
        });
    });
}

// 保存排序
function saveServerOrders() {
    var orderedIds = [];

    $(".server-item").each(function() {
        orderedIds.push($(this).attr("rel"));
    });
            
    $.doPost(admin.ui.basePath+'admin/server/saveorders', {
        orderedIds: orderedIds.join(",")
    }, function(result) {
        if (!result && !result.success) {
            $.box.alert(result.message);
        }
    });
}

// 删除单个日志
function deleteSystemLog(fileName) {
    $.box.confirmx("删除日志", "确定要删除吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/systemlog/delete', {
            fileName: fileName
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        location.reload();
                    }
                });
            }
        });
    });
}
// 删除多个日志
function deleteSystemLogs() {
    var fileNames = [];

    $(".check-box").each(function() {
        var val = $(this).attr("rel");
        if (this.checked)
            fileNames.push(val);
    });

    if (fileNames.length == 0) {
        $.box.alert("请选择至少一个日志！");
        return;
    }

    $.box.confirmx("删除日志", "确定要删除选中的日志吗？", function(v, m, f) {
        $.box.tip("正在处理...");
        $.doPost(admin.ui.basePath+'admin/systemlog/batchdelete', {
            fileNames: fileNames.join(",")
        }, function(result) {
            $.box.close();
            if (!result) {
                $.box.alert("服务器出错！");
            }
            else if (!result.success) {
                $.box.alert(result.message);
            }
            else {
                $.box.tip(result.message, 1500, {
                    closed: function() {
                        location.reload();
                    }
                });
            }
        });
    });
}